package com.isay.app.honey.ui.main;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

import com.isay.app.honey.R;
import com.isay.frameworklib.adapter.MyFragmentPagerAdapter;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.utils.ClickUtils;
import com.isay.frameworklib.utils.ToastUtils;
import com.isay.frameworklib.widget.tab.FragmentTabWidget;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;
    private FragmentTabWidget mMainBottomTab;
    private MyFragmentPagerAdapter mFragmentPagerAdapter;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);

        initView();
    }

    @Override
    protected int getStatusBarColor() {
        return 0;
    }


    private void initView() {
        mViewPager = findViewById(R.id.main_view_pager);
        mMainBottomTab = findViewById(R.id.main_bottom_tab);
        mFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mFragmentPagerAdapter.setFragments(MainUtils.getMainTabFragment());
        mViewPager.setAdapter(mFragmentPagerAdapter);
        mViewPager.addOnPageChangeListener(this);
        mMainBottomTab.addTabAll(MainUtils.getMainTabInfo(this));
        mMainBottomTab.setCurrentTab(0);
        mMainBottomTab.setOnTabReselectedListener(mOnTabSelectedListener);
    }

    //Tab点击监听
    private FragmentTabWidget.OnTabSelectedListener mOnTabSelectedListener = new FragmentTabWidget.OnTabSelectedListener() {
        @Override
        public void onTabSelected(int position) {
            mViewPager.setCurrentItem(position);
        }
    };


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        mMainBottomTab.setCurrentTab(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onBackPressed() {
        if (ClickUtils.isFastClick(2000)) {
            super.onBackPressed();
            ToastUtils.cancel();
        } else {
            ToastUtils.show("再次点击退出");
        }
    }
}
